package leetcode.code1154;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;
import leetcode.helper.MaxDebug;

public class Solution1 implements IDebug {

	private static int[] months1 = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	private static int[] months2 = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	private static int[] monthSum1;
	private static int[] monthSum2;

	static {
		monthSum1 = new int[13];
		monthSum2 = new int[13];
		monthSum1[0] = 0;
		monthSum2[0] = 0;
		for (int i = 1; i <= 12; i++) {
			monthSum1[i] = monthSum1[i - 1] + months1[i - 1];
			monthSum2[i] = monthSum2[i - 1] + months2[i - 1];
		}
	}

	public int dayOfYear(String date) {
		int year = 0, month = 0, day = 0;
		char[] cc = date.toCharArray();
		int p = 0, pos = 0;
		while (p < cc.length) {
			char c = cc[p++];
			if (c == '-') {
				pos += 1;
				continue;
			}
			if (pos == 0) {
				year = year * 10 + c - '0';
			} else if (pos == 1) {
				month = month * 10 + c - '0';
			} else {
				day = day * 10 + c - '0';
			}
		}
		int ans = 0;
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
			ans += monthSum2[month - 1] + day;
		} else {
			ans += monthSum1[month - 1] + day;
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(61, this.dayOfYear("2004-03-01"));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(60, this.dayOfYear("2003-03-01"));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(9, this.dayOfYear("2019-01-09"));

	}

	@Override
	public void debug4() {
		HelpDebug.compare(41, this.dayOfYear("2019-02-10"));

	}
	public void debug5() {
		HelpDebug.compare(212, this.dayOfYear("2001-07-31"));

	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		MaxDebug.debug14(so);
		so.debug5();
	}

}
